import router from '@/router';
import { logout } from '@/api/login';

export default {
  state: {
    isLogin: false,
    customerName: '',
    loginName: '',
    domainId: '',
    roleId: '',
    token: ''
  },
  mutations: {
    SET_LOGIN_STATE: (state, isLogin) => {
      state.isLogin = isLogin;
    },
    SET_CUSTOMER_NAME: (state, name) => {
      state.customerName = name;
    },
    SET_LOGIN_NAME: (state, name) => {
      state.loginName = name;
    },
    SET_DOMAIN_ID: (state, id) => {
      state.domainId = id;
    },
    SET_ROLE_ID: (state, phone) => {
      state.roleId = phone;
    },
    SET_TOKEN: (state, token) => {
      state.token = token;
    }
  },
  actions: {
    setUserInfo({ commit }, data) {
      commit('SET_CUSTOMER_NAME', data.customerName);
      commit('SET_LOGIN_NAME', data.loginName);
      commit('SET_DOMAIN_ID', data.domainId);
      commit('SET_TOKEN', data.token);
      commit('SET_ROLE_ID', data.roleId);
      commit('SET_LOGIN_STATE', true);
      return Promise.resolve();
    },
    logout({ commit }) {
      logout();
      commit('SET_TOKEN', '');
      router.replace('/login');
    }
  }
};
